package com.dz.goods.service;

import com.dz.common.base.BasePageResultsVO;
import com.dz.common.constants.CodeDomain;
import com.dz.goods.domain.entity.Category;
import com.dz.goods.domain.entity.QSpec;
import com.dz.goods.domain.entity.Spec;
import com.dz.goods.domain.entity.Template;
import com.dz.goods.domain.repository.CategoryRepository;
import com.dz.goods.domain.repository.SpecRepository;
import com.dz.goods.domain.vo.SpecParamsVO;
import com.dz.goods.domain.vo.SpecResultsVO;
import com.dz.goods.domain.vo.TemplateResultsVO;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class SpecServiceImpl implements SpecService{

    @Autowired
    private SpecRepository specRepository;
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Transactional
    @Override
    public Spec findById(Long id) {
        return specRepository.findById(id).get();
    }
    @Transactional
    @Override
    public Spec add(Spec spec) {
        return specRepository.saveAndFlush(spec);
    }
    @Transactional
    @Override
    public void delete(Long id) {
        Spec spec = specRepository.findById(id).get();
        spec.setDeleteFlag(Integer.valueOf(CodeDomain.DELETE_FLAG_YES.value()));
        specRepository.saveAndFlush(spec);
    }
    @Transactional
    @Override
    public Spec edit(Spec spec) {
        return specRepository.saveAndFlush(spec);
    }
    @Transactional
    @Override
    public BasePageResultsVO<SpecResultsVO> getSpecList(SpecParamsVO vo) {
        QSpec spec=QSpec.spec;
        BooleanBuilder where=new BooleanBuilder();
        if(StringUtils.isNotEmpty(vo.getId())){
            where.and(spec.id.eq(Long.parseLong(vo.getId())));
        }
        if(StringUtils.isNotEmpty(vo.getName())){
            where.and(spec.name.eq(vo.getName()));
        }
        if(StringUtils.isNotEmpty(vo.getOptions())){
            where.and(spec.options.eq(vo.getOptions()));
        }
        if(StringUtils.isNotEmpty(vo.getTemplateId())){
            where.and(spec.templateId.eq(Long.valueOf(vo.getTemplateId())));
        }
        where.and(spec.deleteFlag.eq(Integer.valueOf(CodeDomain.DELETE_FLAG_NO.value())));
        JPAQuery<Spec> query=jpaQueryFactory.select(spec).from(spec).where(where.getValue());
        QueryResults<Spec> fetchResults = query.offset((vo.getPageNum() - 1L) * vo.getPageSize()).limit(vo.getPageSize()).fetchResults();
        List<SpecResultsVO> list=fetchResults.getResults().stream().map(e->SpecResultsVO.builder()
                .id(e.getId())
                .name(e.getName())
                .options(e.getOptions())
                .templateId(e.getTemplateId()).build()).collect(Collectors.toList());
        return BasePageResultsVO.success(vo.getPageNum(),vo.getPageSize(),fetchResults.getTotal(),list);
    }
    @Transactional
    @Override
    public List<SpecResultsVO> findByCategoryId(Long categoryid) {
        Category category = categoryRepository.findById(categoryid).get();
        QSpec spec=QSpec.spec;
        BooleanBuilder where=new BooleanBuilder();
        where.and(spec.templateId.eq(category.getTemplateId()));
        where.and(spec.deleteFlag.eq(Integer.valueOf(CodeDomain.DELETE_FLAG_NO.value())));
        List<Spec> specList = jpaQueryFactory.select(spec).from(spec).where(where.getValue()).fetch();
        List<SpecResultsVO> resultsVOS = specList.stream().map(e -> SpecResultsVO.builder().id(e.getId())
                .name(e.getName())
                .options(e.getOptions())
                .templateId(e.getTemplateId())
                .build()).collect(Collectors.toList());
        return resultsVOS;
    }


}